#include "Camera.h"
#include "Transform.h"
#include "Vector.h"

using MathGraphics::Transform;
using MathGraphics::scale;
using MathGraphics::translate;
using MathGraphics::Vector;

namespace CameraInterface
{
	Camera::Camera(int width, int height, Transform projection, Transform worldToCamera)
	{
		xres = width;
		yres = height;

		double ratio = (double)width / (double)height;
		double screen[4];

		screen[0] = -ratio; 
		screen[1] = ratio;
		screen[2] = 1.0;
		screen[3] = -1.0;

		cameraToScreen = projection;
		Transform screenToCamera = Transform(cameraToScreen.inv, cameraToScreen.mat);

		screenToRaster = scale(xres, yres, 1.0) *  
						 scale(1.0 / (screen[1] - screen[0]), 1.0 / (screen[2] - screen[3]), 1.0) * 
		                 translate(Vector(-screen[0], -screen[3], 0));

		rasterToScreen = Transform(screenToRaster.inv, screenToRaster.mat);

		rasterToCamera = screenToCamera * rasterToScreen;

		cameraToWorld = Transform(worldToCamera.inv, worldToCamera.mat);
	}
}